package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.constant.BaseContextConstant;
import com.heima.context.BaseContext;
import com.heima.dto.InsertBusinessDto;
import com.heima.dto.OpportunityDto;
import com.heima.dto.OpportunitySelectDto;
import com.heima.dto.pagedto.PageOpportunityDto;
import com.heima.mapper.OpportunityMapper;
import com.heima.service.OpportunityService;
import com.heima.vo.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class OpportunityServiceImpl implements OpportunityService {

    private  final OpportunityMapper opportunityMapper;

    /**
     * 商机分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult queryOpportunity(PageOpportunityDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        Page<OpportunityVo> page = opportunityMapper.queryOpportunity(dto);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRows(page.getResult());
        return pageResult;
    }

    /**
     * 新增商机
     * @param dto
     */
    @Override
    public void insertBusiness(InsertBusinessDto dto) {
        dto.setAssignBy((String) BaseContext.getData(BaseContextConstant.USERNAME));
        dto.setOwner((String) BaseContext.getData(BaseContextConstant.USERNAME));
        opportunityMapper.insertBusiness(dto);
    }

    /**
     * 根据id查询商机
     * @param id
     * @return
     */
    @Override
    public BusinessIdListVo findBusinessById(Integer id) {
        BusinessInfoVo info = opportunityMapper.findBusinessById(id);
        BusinessIdListVo vo = new BusinessIdListVo();
        vo.setData(info);
        return vo;
    }

    /**
     * 查询商机的跟进详情
     * @param businessId
     * @return
     */
    @Override
    public BusinessIdListVo businessIdListVo(Integer businessId) {
        BusinessInfoVo vo = opportunityMapper.businessIdListVo(businessId);

        BusinessIdListVo businessInfoVo = new BusinessIdListVo();
        businessInfoVo.setData(vo);
        return businessInfoVo;
    }

    /**
     * 获取商机历史沟通记录
     * @param id
     * @return
     */
    @Override
    public BusinessIdInfoListVo businessIdInfoVo(Integer id) {
        List<BusinessIdInfoVo> list = opportunityMapper.businessIdInfoVo(id);
        BusinessIdInfoListVo vo = new BusinessIdInfoListVo();
        vo.setData(list);
        return vo;
    }

    /**
     * 跟进商机并修改
     * @param dto
     */
    @Override
    public void recordBusiness(OpportunityDto dto) {

        BusinessIdInfoVo bii = new BusinessIdInfoVo();
        bii.setBusinessId(dto.getBusinessId());
        bii.setRecord(dto.getRecord());
        bii.setKeyItems(dto.getKeyItems());
        bii.setCreateBy((String) BaseContext.getData(BaseContextConstant.USERNAME));
        bii.setCreateTime(String.valueOf(LocalDateTime.now()));
        bii.setNextTime(dto.getNextTime());
        bii.setTrackStatus(dto.getTrackStatus());
        opportunityMapper.insertTrace(bii);
//        opportunityMapper.updateBusiness(dto);

    }

    /**
     * 商机池
     * @param dto
     * @return
     */
    @Override
    public PageResult queryPool(PageOpportunityDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        Page<OpportunityVo> page = opportunityMapper.queryPool(dto);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRows(page.getResult());
        return pageResult;
    }

    @Override
    public void assignment(OpportunitySelectDto opportunitySelectDto) {
        // 设置分配者与分配时间, 分配者为 当前操作用户, 分配时间为 当前时间
        // 设置所属者和所属时间, 所属者 opportunitySelectDto.userId , 所属时间为 当前时间
        String assignBy = BaseContext.getData(BaseContextConstant.USERNAME).toString();
        opportunityMapper.assignment(opportunitySelectDto, assignBy);
    }
}
